package cn.mw.cmdb.template.strategy.handler;

import cn.mw.cmdb.exception.ZabbixResultException;
import cn.mw.cmdb.template.constant.MwTemplateConfigurationConstant;
import cn.mw.cmdb.template.enums.TemplateDetailEnum;
import cn.mw.cmdb.template.enums.ZabbixMonitorTypeEnum;
import cn.mw.cmdb.template.param.MonitorItemParam;
import cn.mw.cmdb.template.param.MwTemplateConfigurationParam;
import cn.mw.cmdb.template.strategy.TemplateDetailHandler;
import cn.mw.cmdb.template.utils.TemplateSortUtil;
import cn.mw.microMonitorCommon.constants.ZabbixApiConstants;
import cn.mw.microMonitorCommon.constants.ZabbixMethodConstants;
import cn.mw.microMonitorCommon.entity.zabbix.MWZabbixAPIResult;
import cn.mw.microMonitorCommon.util.ListMapObjUtils;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description 监控项原型处理
 */
@Service
@Slf4j
public class MonitorItemPrototypeHandler extends TemplateDetailHandler {

    private final String discoveryIds = "discoveryids";
    @Override
    public TemplateDetailEnum handlerType() {
        return TemplateDetailEnum.ITEM_PROTOTYPE;
    }

    @Override
    public Object getData(MwTemplateConfigurationParam param) {
        Map zabbixParam = new HashMap();
        zabbixParam.put(discoveryIds,Arrays.asList(param.getRuleId()));
        zabbixParam.put(MwTemplateConfigurationConstant.OUTPUT,MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectTags,MwTemplateConfigurationConstant.EXTEND);
        MWZabbixAPIResult zabbixAPIResult = zabbixDubboService.getZabbixApiResult(ZabbixApiConstants.GET_CUSTOM_INFO_BY_METHOD, param.getServerId(), ZabbixMethodConstants.ITEM_PROTOTYPE_GET, zabbixParam);
        if(zabbixAPIResult == null || zabbixAPIResult.isFail()){
            throw new ZabbixResultException(String.valueOf(zabbixAPIResult.getData()));
        }
        List<MonitorItemParam> monitorItemDtos = JSONArray.parseArray(String.valueOf(zabbixAPIResult.getData()), MonitorItemParam.class);
        if(CollectionUtils.isNotEmpty(monitorItemDtos)){
            for (MonitorItemParam monitorItemDto : monitorItemDtos) {
                String itemType = monitorItemDto.getType();
                ZabbixMonitorTypeEnum monitorTypeEnum = ZabbixMonitorTypeEnum.getMonitorTypeEnum(itemType);
                if(monitorItemDto != null){
                    monitorItemDto.setType(monitorTypeEnum.getName());
                }
            }
        }
        if(CollectionUtils.isNotEmpty(monitorItemDtos) && StringUtils.isNotEmpty(param.getDetailName())){
            monitorItemDtos = monitorItemDtos.stream().filter(item->item.getName().contains(param.getDetailName())).collect(Collectors.toList());
        }
        //数据排序
        List realData = TemplateSortUtil.templateSortList(monitorItemDtos,param,param.getQuery());
        if(param.getPageNum() == null || param.getPageSize() == null){
            return realData;
        }
        Page page = pageHandler(realData, param.getPageNum(), param.getPageSize());
        return page;
    }

    @Override
    public Object createData(MwTemplateConfigurationParam param) {
        Object detailData = param.getDetailData();
        MonitorItemParam monitorItemParam = JSONObject.parseObject(JSONObject.toJSONString(detailData), MonitorItemParam.class);
        tagHandler(monitorItemParam);
        Map zabbixParam = ListMapObjUtils.beanToMap(monitorItemParam);
        delMapNullValue(zabbixParam);
        MWZabbixAPIResult zabbixAPIResult = zabbixDubboService.getZabbixApiResult(ZabbixApiConstants.GET_CUSTOM_INFO_BY_METHOD, param.getServerId(), ZabbixMethodConstants.ITEM_PROTOTYPE_CREATE, zabbixParam);
        if(zabbixAPIResult == null || zabbixAPIResult.isFail()){
            throw new ZabbixResultException(String.valueOf(zabbixAPIResult.getData()));
        }
        return null;
    }

    private void tagHandler(MonitorItemParam monitorItemParam) {
        List<Map> tags = monitorItemParam.getTags();
        if(CollectionUtils.isEmpty(tags)){return;}
        List<String> applications = new ArrayList<>();
        for (Map tag : tags) {
            if(tag.containsKey(applicationId)){
                applications.add(tag.get(applicationId).toString());
            }
        }
        monitorItemParam.setApplications(applications);
    }

    @Override
    public Object updateData(MwTemplateConfigurationParam param) {
        Object detailData = param.getDetailData();
        MonitorItemParam monitorItemParam = JSONObject.parseObject(JSONObject.toJSONString(detailData), MonitorItemParam.class);
        tagHandler(monitorItemParam);
        Map zabbixParam = ListMapObjUtils.beanToMap(monitorItemParam);
        delMapNullValue(zabbixParam);
        MWZabbixAPIResult zabbixAPIResult = zabbixDubboService.getZabbixApiResult(ZabbixApiConstants.GET_CUSTOM_INFO_BY_METHOD, param.getServerId(), ZabbixMethodConstants.ITEM_PROTOTYPE_UPDATE, zabbixParam);
        if(zabbixAPIResult == null || zabbixAPIResult.isFail()){
            throw new ZabbixResultException(String.valueOf(zabbixAPIResult.getData()));
        }
        return null;
    }

    @Override
    public Object deleteData(MwTemplateConfigurationParam param) {
        MWZabbixAPIResult zabbixAPIResult = zabbixDubboService.getZabbixApiResult(ZabbixApiConstants.GET_CUSTOM_INFO_BY_METHOD, param.getServerId(), ZabbixMethodConstants.ITEM_PROTOTYPE_DELETE, param.getDetailDeleteIds());
        if(zabbixAPIResult == null || zabbixAPIResult.isFail()){
            throw new ZabbixResultException(String.valueOf(zabbixAPIResult.getData()));
        }
        return null;
    }

    @Override
    public Object selectDataById(MwTemplateConfigurationParam param) {
        //根据监控项ID查询数据
        Map zabbixParam = new HashMap();
        zabbixParam.put(MwTemplateConfigurationConstant.ITEM_IDS,Arrays.asList(param.getDetailId()));
        zabbixParam.put(MwTemplateConfigurationConstant.OUTPUT,MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectTags,MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectApplications,MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectPreprocessing,MwTemplateConfigurationConstant.EXTEND);
        MWZabbixAPIResult zabbixAPIResult = zabbixDubboService.getZabbixApiResult(ZabbixApiConstants.GET_CUSTOM_INFO_BY_METHOD, param.getServerId(), ZabbixMethodConstants.ITEM_PROTOTYPE_GET, zabbixParam);
        if(zabbixAPIResult == null || zabbixAPIResult.isFail()){
            throw new ZabbixResultException(String.valueOf(zabbixAPIResult.getData()));
        }
        List<MonitorItemParam> monitorItemParams = JSONArray.parseArray(String.valueOf(zabbixAPIResult.getData()), MonitorItemParam.class);
        if(CollectionUtils.isEmpty(monitorItemParams)){return null;}
        return monitorItemParams.get(0);
    }
}
